'use strict';
const db=uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	if(event.leixing==0){
		let result = await uniCloud.deleteFile({
		    fileList: [
		       event.picurl
		    ],
			success(res){
			},
			fail(err){
			}
		});
		return result
	}else if(event.leixing==1){//修改轮播图
		let result=await db.collection('banner').where({_id:event.id}).update({
			url:event.url,
			biaoti:event.biaoti,
			lianjie:event.lianjie,
			time:event.time
		})
		return result
	}else if(event.leixing==2){//提交轮播图
		let result=await db.collection('banner').add({
			url:event.url,
			biaoti:event.biaoti,
			lianjie:event.lianjie,
			time:event.time
		})
		return result
	}else if(event.leixing==3){
		const res= await db.collection('banner').get()
		return {res}
	}else if(event.leixing==4){
		const res= await db.collection('banner').where({_id:event.id}).remove()
	}else if(event.leixing==5){//修改教务老师
		const res= await db.collection('uni-id-users').where({_id:event.id}).update({
			name:event.name,
			mobile:event.tel,
			username:event.tel,
			avatar:event.zhaopian,
			"avatar_file.url":event.zhaopian,
			role:event.role,
			jingshouren:event.jingshouren
		})
		let res1=await db.collection('xiugaishuju').add({
			detail:event.detail,
			riqi:event.riqi,
			jingshouren:event.jingshouren,
			leibie:'修改老师数据'
		})
	}else if(event.leixing==6){//添加老师信息
		let result=await db.collection('uni-id-users').add(event.users)
		let res=await db.collection('xiugaishuju').add(event.xiugai_shuju)
		return res
	}else if(event.leixing==7){//删除老师信息
		let result=await db.collection('uni-id-users').where({_id:event.id}).remove()
		let res=await db.collection('xiugaishuju').add({
			detail:event.detail,
			riqi:event.riqi,
			jingshouren:event.jingshouren,
			leibie:'删除老师数据'
		})
		return result
	}else if(event.leixing==8){
		let result=await db.collection('kecheng').where({_id:event.id}).remove()
		let res=await db.collection('xiugaishuju').add({
			detail:event.detail,
			riqi:event.riqi,
			jingshouren:event.jingshouren,
			leibie:'删除课程数据',
			xiaoqu:event.xiaoqu
		})
		return result
	}else if (event.leixing == 9) {
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		dbJQL.setUser({ // 指定后续执行操作的用户信息，此虚拟用户将同时拥有传入的uid、role、permission
			// uid: 'user-id', // 用户id
			role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份，可以删除uid和permission字段
			// permission: [] // 用户权限
		})
		const order = await dbJQL.collection('shangpin').where({
			xiaoqu:dbCmd.in(event.xiaoqu),
			name: new RegExp(event.key)
		}).getTemp() // 临时表field方法内需要包含关联字段，否则无法建立关联关系
		const book = await dbJQL.collection('shangpin_fenlei')
		// .field(	'_id,name,zhaopian,age,nianji,school,tel,zhaopian,zhuzhi')
			.get() // 临时表field方法内需要包含关联字段，否则无法建立关联关系
		const res = await dbJQL.collection( 'shangpin_fenlei',order).get() // 注意collection方法内需要传入所有用到的表名，用逗号分隔，主表需要放在第一位
		return {res,book}
		
		
		
		// const res = await db.collection('shangpin').where({
		// 	xiaoqu:dbCmd.in(event.xiaoqu),
		// 	name: new RegExp(event.key)
		// }).get()
		// return res
	}else if (event.leixing == 10) { //
		const res = await db.collection('baokelist').doc(event.id).remove()
		const res1 = await db.collection('xiugaishuju').add({
			detail:event.detail,
			riqi:event.riqi,
			jingshouren:event.jingshouren,
			leibie:'删除报课记录',
			xiaoqu:event.xiaoqu
		})
		const res2 = await db.collection('zhangwuliushui').add({
			jine:event.jine,
			biandong:'支出',
			leibie:'删除报课记录',
			detail:event.detail,
			riqi:event.riqi,
			jingshouren:event.jingshouren,
			xiaoqu:event.xiaoqu
		})
		return res
	}else if (event.leixing == 11) { //
		const res = await db.collection('baokelist').doc(event.id).update({
			isxufei:0
		})
		return res
	}else if (event.leixing == 12) { //类型为10时，获取教务老师数据
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		dbJQL.setUser({ // 指定后续执行操作的用户信息，此虚拟用户将同时拥有传入的uid、role、permission
			// uid: 'user-id', // 用户id
			role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份，可以删除uid和permission字段
			// permission: [] // 用户权限
		})
		const res = await dbJQL.collection('uni-id-users').where(event.tiaojian).field('mobile,name,avatar,my_invite_code,tuijian_lian').get()
		return res
	}else if(event.leixing==13){
		const res= await db.collection('fenxianghaibao').get()
		return res
	}
};
